package org.edu.modules.center.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.center.entity.VeKfcStudentApply;
import org.edu.modules.center.service.IVeKfcStudentApplyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"学员申请信息表"})
@RestController
@RequestMapping({"/center/veKfcStudentApply"})
public class VeKfcStudentApplyController
        extends BaseController<VeKfcStudentApply, IVeKfcStudentApplyService>
{
    private static final Logger log = LoggerFactory.getLogger(VeKfcStudentApplyController.class);
    @Autowired
    private IVeKfcStudentApplyService veKfcStudentApplyService;

    @AutoLog("学员申请信息表-分页查询")
    @ApiOperation(value="分页查询", notes="学员申请信息表-分页查询")
    @PostMapping({"/list"})
    public Result<?> queryPageList(VeKfcStudentApply veKfcStudentApply, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req)
    {
        if (veKfcStudentApply.getStudentid().equals(Integer.valueOf(0)))
        {
            LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
//            veKfcStudentApply.setStudentid(sysUser.getUserid());
        }
        QueryWrapper<VeKfcStudentApply> queryWrapper = QueryGenerator.initQueryWrapper(veKfcStudentApply, req.getParameterMap());
        Page<Map<String, Object>> page = new Page(pageNo.intValue(), pageSize.intValue());
        queryWrapper.setEntity(veKfcStudentApply);
        IPage<Map<String, Object>> pageList = this.veKfcStudentApplyService.pageMaps(page, queryWrapper);
        return Result.OK(pageList);
    }

    @AutoLog("学员申请信息表-通过id查询")
    @ApiOperation(value="通过id查询", notes="学员申请信息表-通过id查询")
    @PostMapping({"/queryById"})
    public Result<?> queryById(@RequestParam(name="id", required=true) String id)
    {
        VeKfcStudentApply veKfcStudentApply = (VeKfcStudentApply)this.veKfcStudentApplyService.getById(id);
        if (veKfcStudentApply == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veKfcStudentApply);
    }

    @AutoLog("学员申请信息表-新增信息")
    @ApiOperation(value="新增信息", notes="学员申请信息表-新增信息")
    @PostMapping({"/add"})
    @Transactional
    public Result<?> add(@RequestBody VeKfcStudentApply veKfcStudentApply)
    {
        Integer courseId = veKfcStudentApply.getCourseid();
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        int studentId = 0;
//        if ((sysUser != null) && (sysUser.getUserid() != null)) {
//            studentId = sysUser.getUserid().intValue();
//        }
        Date createTime = new Date();
        veKfcStudentApply.setCourseid(courseId);
        veKfcStudentApply.setStudentid(Integer.valueOf(studentId));
        veKfcStudentApply.setCreatetime(createTime);
        veKfcStudentApply.setStatus(Integer.valueOf(1));
        this.veKfcStudentApplyService.save(veKfcStudentApply);
        return null;
    }
}
